<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_Index_Base extends Controller
{
    //protected $title  = 'Recensee';
	protected $_layout = 'index/layout_tpl';
    
    public function before()
    {
        parent::before();
        
        $this->_detect_company(); 
        $this->_get_params();   
    }
    
    protected function _get_params()
    {
        $changed = false;
        $url     = $_SERVER['REQUEST_URI'];
        if (isset($_GET['ref']) && ($ref = $_GET['ref'])) {
            setcookie('ref', $ref, time()+30*24*60*60, '/');     // 30 дней 
            
            $url = str_replace(array('?ref='.$ref, '&ref='.$ref), array('', ''), $url); 
            $changed = true;
        }
        
        if ($changed) {
            Helper_Index::redirect_301($url);    
        }
    }
    
    protected function _detect_company()
    {
        $host  = $_SERVER['HTTP_HOST'];
        $parts = explode('.', $host);
        
        if (count($parts) == 3) {
            $domain = $parts[0];
            $company = ORM::factory('company')->get_company_by_domain($domain);
            $company->set_as_current();
        }
    }
	
	public function print_template($title, $content)
	{
        $this->_layout = new View($this->_layout);

		$this->_layout->title       = $title;
		$this->_layout->content     = $content;
		
		$this->response->body($this->_layout);
	}

}
